package com.cloudptt.api.product.func.lbs;

import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.product.config.lbs.ILocalConfigListener;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.lbs.LBSService.LBSBinder;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.ServerInfo;
import com.mcptt.db.op.MapDbOperator;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.IBinder;

public class LBSFunc implements IFunc,ILocalConfigListener{

	public static String Log_Key = "LBSFunc";
	private Context ctx = null;

	private LBSBroadcast broadcast;
//	private LBSOperator operator = null;
	private LocationConfig localConfig = null;

//	private LbsListener listener = null;
	private MapDbOperator dpOperator = null;

	public LBSFunc(Context ct,LocationConfig localConfig,MapDbOperator dpOperator) {
		Log.d(Log_Key, "LBSFunc localConfig = " + localConfig);
		this.ctx = ct;
//		this.operator = operator;
		this.localConfig = localConfig;
		this.localConfig.setListener(this);
		this.dpOperator = dpOperator;


		ctx.getApplicationContext().bindService(
				new Intent(ctx, LBSService.class), conn,
				Context.BIND_AUTO_CREATE);

		broadcast = new LBSBroadcast(ctx);		
	}


	public LBSService lbs = null;
	ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder binder) {

			Log.d(Log_Key, " onServiceConnected start lbsservice");
			LBSBinder lbsbinder = (LBSBinder) binder;
			lbs = lbsbinder.getService();
//			lbs.setOperatoer(operator);
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// 停止服务
			lbs.stopLBS();
			lbs = null;
		}
	};
	private boolean isLogined;

	/*	@Override
	public void recvRequest(String mcpttId, LocationInfoBody_Request body) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, " recvRequest mcpttId = " + mcpttId + " body = " + body);
		if(lbs != null
				&& mcpttId != null)
		{
			lbs.reportLocation(mcpttId,false);
		}
	}

	@Override
	public void recvConfiguration(String mcpttId,LocationInfoBody_Configuration body) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, " recvConfiguration mcpttId = " + mcpttId + " body = " + body);
		if(localConfig != null
				&& mcpttId != null)
		{
			localConfig.updateConfig(mcpttId,body);
			//lbs.updateConfig(ctx, mcpttId, localConfig);
		}
	}*/

	@Override
	public void onConfigChangeByUser()
	{
		Log.d(Log_Key, "onConfigChangeByUser localConfig = " + localConfig);
	}

	@Override
	public void onLogined(McIdInfo mcIdInfo,ServerInfo info,boolean isLoginSuc)
	{
		Log.d(Log_Key, "onLogined mcIdInfo = " + mcIdInfo);
		Log.d(Log_Key, "onLogined isLogined = " + isLogined + " lbs = " + lbs);
		if(lbs != null && !isLogined)
		{
			if(dpOperator != null)
			{
				dpOperator.cleanAllLogin();
			}

			LocationManager lm = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
			if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
				lbs.startLBS(localConfig);
			}
		}
		isLogined = true;
	}

	@Override
	public void doNetworkChange(boolean isConn) 
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);

		if (!isConn) 
		{
			if(lbs != null)
			{
				lbs.stopLBS();
			}
			if(dpOperator != null)
			{
				dpOperator.cleanAllLogin();
			}
			isLogined = false;
		}
		//		else
		//		{
		//			lbs.startLBS(localConfig);
		//		}

	}

	@Override
	public void doExit() 
	{
		Log.d(Log_Key, "doExit");
		if(ctx != null)
		{
			this.ctx.getApplicationContext().unbindService(conn);
		}
		doLogout();

		clearResource();
	}

	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		doLogout();
	}
	@Override
	public void doLogout() {
		Log.d(Log_Key, "doLogout");
		if(lbs != null)
		{
			lbs.stopLBS();
		}
		if(dpOperator != null)
		{
			dpOperator.cleanAllLogin();
		}

		//clearResource();

		isLogined = false;
	}

	private void clearResource()
	{
		if(broadcast!=null)
		{
			broadcast.clearResource();	
			broadcast = null;
		}
		if(dpOperator != null)
		{
			dpOperator.cleanAllLogin();
			dpOperator = null;
		}
		this.ctx = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}

	public void startServer(){
		if(lbs != null)
		{
			lbs.startLBS(localConfig);
		}
	}

	public void stopServer(){
		if(lbs != null)
		{
			lbs.stopLBS();
		}
	}

//	@Override
//	public void clearBtData() {
//		// TODO Auto-generated method stub
//		if(lbs != null)
//		{
//			lbs.clearBtData();
//		}
//	}
	
	public LocationData getCurLocation()
	{
		if(lbs != null)
		{
			return lbs.getCurLocation();
		}
		return LocationData.getDefaultLocationData();
	}
	
//	public VitalData getCurVitalData()
//	{
//		if(lbs != null)
//		{
//			return lbs.getCurVitalData();
//		}
//		return null;
//	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onKeepLive isconnected = " + isconnected);

		if(!isconnected)
		{
			if(lbs != null)
			{
				lbs.stopLBS();
			}
			isLogined = false;
		}
		else
		{
			Log.d(Log_Key, "onKeepLive isLogined = " + isLogined + " lbs = " + lbs);
			if(lbs != null && !isLogined)
			{
				if(dpOperator != null)
				{
					dpOperator.cleanAllLogin();
				}

				LocationManager lm = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
				if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
					lbs.startLBS(localConfig);
				}
			}
			isLogined = true;
		}
	}
}
